package com.sinodata.bsm.cicp.collector.informix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: informix数据库实例概要信息采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-1-8 下午2:46:18          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class InstanceProfileCollector extends Collector {

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15" };
    }

    @Override
    public String[] getParamMeta() {
        return null;
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String environment = this.getParamMap().get("Env");
        String shell = "onstat -p";
        if (environment == null || environment.trim().length() == 0) {
            String hostType = CommandReader.getHostType(ip);
            if (hostType != null) {
                shell = (hostType.equalsIgnoreCase("Linux") ? ". ./.bash_profile;" : ". ./.profile;") + shell;
            }
        } else {
            shell = environment + ";" + shell;
        }
        String[] propMetas = this.getPropMeta();
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue resPropertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        resPropertyValueX.setValues(propValues);
        values.add(resPropertyValueX);
        BufferedReader reader = null;
        String strLine = null;
        List<String> list = new ArrayList<String>();
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                if (strLine.indexOf("dskreads") > -1) {
                    strLine = reader.readLine();
                    String[] ss = strLine.split("\\s+");
                    if (ss != null && ss.length == 8) {
                        list.add(ss[3]);
                        list.add(ss[7]);
                        continue;
                    }
                }
                if (strLine.indexOf("isamtot") > -1) {
                    strLine = reader.readLine();
                    String[] ss = strLine.split("\\s+");
                    if (ss != null && ss.length == 9) {
                        list.add(ss[0]);
                        list.add(ss[7]);
                        list.add(ss[8]);
                    }
                    continue;
                }
                if (strLine.indexOf("ovlock") > -1) {
                    strLine = reader.readLine();
                    String[] ss = strLine.split("\\s+");
                    if (ss != null && ss.length == 7) {
                        list.add(ss[0]);
                        list.add(ss[2]);
                    }
                    continue;
                }
                if (strLine.indexOf("bufwaits") > -1) {
                    strLine = reader.readLine();
                    String[] ss = strLine.split("\\s+");
                    if (ss != null && ss.length == 8) {
                        list.add(ss[0]);
                        list.add(ss[1]);
                        list.add(ss[2]);
                        list.add(ss[7]);
                    }
                    continue;
                }

                if (strLine.indexOf("ixda-RA") > -1) {
                    strLine = reader.readLine();
                    String[] ss = strLine.split("\\s+");
                    if (ss != null && ss.length == 5) {
                        list.add(ss[0]);
                        list.add(ss[1]);
                        list.add(ss[2]);
                        list.add(ss[3]);
                    }
                }
            }
            if (list.size() < 15) {
                return null;
            }
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(list.get(i), propMetas[i]);
            }
        } catch (IOException e) {
            logger.error("failed to collect informix instance profile", e);
            throw new CollectorException(e);
        }
        return values;
    }
}
